package com.cloud.pole.shop.config;

import com.cloud.pole.common.constant.RebbitConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

/*
*2019年5月18日
*author:frank-chen
*/
@Configuration
public class RabbitConfiguration {

    /**
     * 信道配置
     * @return
     */
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(RebbitConstants.REBBIT_SHOP_ORDER_EXCHANGE, true, false);
    }

    /*
    业务队列定义与绑定
     */
    @Bean
    public Queue queue() {
        Queue queue = new Queue(RebbitConstants.REBBIT_SHOP_ORDER,true);
        return queue;
    }

    @Bean
    public Binding binding() {
        //队列绑定到exchange上，再绑定好路由键
        return BindingBuilder.bind(queue()).to(defaultExchange()).with(RebbitConstants.REBBIT_SHOP_ORDER);
    }

    //下面是延迟队列的配置
    //转发队列
    @Bean
    public Queue repeatTradeQueue() {
        Queue queue = new Queue(RebbitConstants.REBBIT_SHOP_ORDER_REPEAT_TRADE,true,false,false);
        return queue;
    }

    //绑定转发队列
    @Bean
    public Binding  drepeatTradeBinding() {
        return BindingBuilder.bind(repeatTradeQueue()).to(defaultExchange()).with(RebbitConstants.REBBIT_SHOP_ORDER_REPEAT_TRADE);
    }

    //死信队列  -- 消息在死信队列上堆积，消息超时时，会把消息转发到转发队列，转发队列根据消息内容再把转发到指定的队列上
    @Bean
    public Queue deadLetterQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RebbitConstants.REBBIT_SHOP_ORDER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", RebbitConstants.REBBIT_SHOP_ORDER_REPEAT_TRADE);
        Queue queue = new Queue(RebbitConstants.REBBIT_SHOP_ORDER_DEAD_LETTER,true,false,false,arguments);
        return queue;
    }

    //绑定死信队列
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(defaultExchange()).with(RebbitConstants.REBBIT_SHOP_ORDER_DEAD_LETTER);
    }
    
    //订单支付成功队列
    @Bean
    public Queue sendOrderPaySucceed() {
        return new Queue(RebbitConstants.REBBIT_SHOP_ORDER_PAY_SUCCEED);
    }
}
